package com.shqcjd.sinopecdemomobile.ui.activity.problem;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.shqcjd.sinopecdemomobile.R;
import com.shqcjd.sinopecdemomobile.api.ApiService;
import com.shqcjd.sinopecdemomobile.helper.rxjavahelper.RxObserver;
import com.shqcjd.sinopecdemomobile.helper.rxjavahelper.RxSchedulersHelper;
import com.shqcjd.sinopecdemomobile.model.ResponseData;
import com.shqcjd.sinopecdemomobile.model.pojo.ProblemDetailBean;
import com.shqcjd.sinopecdemomobile.model.pojo.UploadFileRet;
import com.shqcjd.sinopecdemomobile.ui.activity.preview.PreImageActivity;
import com.shqcjd.sinopecdemomobile.ui.activity.preview.VideosActivity;
import com.shqcjd.sinopecdemomobile.ui.adapter.ProblemDetailAdapter;
import com.shqcjd.sinopecdemomobile.ui.adapter.ProblemItemImgAdapter;
import com.shqcjd.sinopecdemomobile.ui.base.BaseActivity;
import com.shqcjd.sinopecdemomobile.ui.base.BasePresenter;
import com.shqcjd.sinopecdemomobile.util.ToastUtils;
import com.shqcjd.sinopecdemomobile.util.UIUtils;

import java.io.Serializable;

import butterknife.BindView;
import butterknife.OnClick;
import io.reactivex.disposables.Disposable;

/**
 * 问题详情
 */
public class ProblemDetailsActivity extends BaseActivity {


    @BindView(R.id.iv_toolbar_back)
    ImageView ivToolbarBack;
    @BindView(R.id.tv_toolbar_center)
    TextView tvToolbarCenter;
    @BindView(R.id.rv_expert_list)
    RecyclerView rv_expert_list;
    @BindView(R.id.tv_obj_name)
    TextView tvObjName;
    @BindView(R.id.tv_user_name)
    TextView tvUserName;
    @BindView(R.id.tv_time)
    TextView tvTime;
    @BindView(R.id.tv_kks)
    TextView tvKks;
    @BindView(R.id.tv_user_code)
    TextView tvUserCode;
    @BindView(R.id.tv_problem_info)
    TextView tvProblemInfo;
    @BindView(R.id.rv_problem_video_img)
    RecyclerView rvProblemVideoImg;
    @BindView(R.id.ll_problem_layout)
    LinearLayout llProblemLayout;
    @BindView(R.id.tv_manager_info)
    TextView tvManagerInfo;
    @BindView(R.id.rv_manager_video_img)
    RecyclerView rvManagerVideoImg;
    @BindView(R.id.ll_manager_layout)
    LinearLayout llManagerLayout;
    @BindView(R.id.tv_manager_time)
    TextView tv_manager_time;

    private Context mContext;
    private ProblemDetailAdapter expertProblemDetailAdapter;
    private int problemFeedbackId;

    @Override
    protected BasePresenter createPresenter() {
        return null;
    }

    @Override
    protected int provideContentViewId() {
        return R.layout.activity_problem_details;
    }

    @Override
    public void initView() {
        mContext = this;
        tvToolbarCenter.setText("问题详情");
        problemFeedbackId = getIntent().getIntExtra("problemFeedbackId", -1);
        expertProblemDetailAdapter = new ProblemDetailAdapter();
        rv_expert_list.setLayoutManager(new LinearLayoutManager(this));
        rv_expert_list.setAdapter(expertProblemDetailAdapter);


    }

    @Override
    public void initData() {
        super.initData();
        getProblemDetail();
    }

    private void getProblemDetail() {
        ApiService.problemDetails(problemFeedbackId)
                .compose(RxSchedulersHelper.io_main())
                .subscribe(new RxObserver<ResponseData<ProblemDetailBean>>() {
                    @Override
                    public void _onSubscribe(Disposable d) {
                        super._onSubscribe(d);
                        showWaitingDialog("正在获取数据...");
                    }

                    @Override
                    public void _onNext(ResponseData<ProblemDetailBean> retData) {
                        if (retData.retCode == 10000) {
                            setView(retData.data);
                        } else {
                            ToastUtils.showShort(mContext, "获取数据失败");
                        }
                    }

                    @Override
                    public void _onError(String errorMessage) {
                        hideWaitingDialog();
                        ToastUtils.showShort(mContext, errorMessage);
                    }

                    @Override
                    public void _onComplete() {
                        super._onComplete();
                        hideWaitingDialog();
                    }
                });
    }

    private void setView(ProblemDetailBean data) {
        tvObjName.setText("项目名称:"+data.projectName);
        tvUserName.setText("上报人员:"+data.executorUserName);
        tvTime.setText("时间:"+data.startTime);
        tvKks.setText("设备编号:"+data.kks);
        tvUserCode.setText("工号:"+data.executorUserCode);
        tv_manager_time.setText(UIUtils.isNull(data.managerReplyTime));
        tvProblemInfo.setText("\u3000\u3000"+data.problemDescription);
        tvManagerInfo.setText("\u3000\u3000"+data.managerReply);

        expertProblemDetailAdapter.setNewData(data.expertList);

        /*问题描述图片列表*/
        ProblemItemImgAdapter problemImgAdapter = new ProblemItemImgAdapter(data.problemAttachments, mContext);
        rvProblemVideoImg.setLayoutManager(new GridLayoutManager(mContext,3));
        rvProblemVideoImg.setAdapter(problemImgAdapter);
        problemImgAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                UploadFileRet item = (UploadFileRet) adapter.getItem(position);
                if(adapter.getData().size()>9&&position==8){
                    Intent intent = new Intent(mContext, ProblemMoreFileActivity.class);
                    Bundle bundle=new Bundle();
                    bundle.putSerializable("list",(Serializable)data.problemAttachments);
                    intent.putExtras(bundle);
                    startActivity(intent);
                }else{
                    if(item.extension.equals(".mp4")){
                        Intent it = new Intent(mContext, VideosActivity.class);
                        it.putExtra("url", item.url);
                        it.putExtra("name", "视频详情");
                        startActivity(it);
                    }else{
                        Intent it = new Intent(mContext, PreImageActivity.class);
                        it.putExtra("url", item.url);
                        it.putExtra("name", "图片详情");
                        startActivity(it);
                    }

                }
            }
        });

        /*片区经理图片列表*/
        ProblemItemImgAdapter managerImgAdapter  = new ProblemItemImgAdapter(data.managerReplyAttachments , mContext);
        rvManagerVideoImg.setLayoutManager(new GridLayoutManager(mContext,3));
        rvManagerVideoImg.setAdapter(managerImgAdapter);
        managerImgAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                UploadFileRet item = (UploadFileRet) adapter.getItem(position);
                if(adapter.getData().size()>9&&position==8){
                    Intent intent = new Intent(mContext, ProblemMoreFileActivity.class);
                    Bundle bundle=new Bundle();
                    bundle.putSerializable("list",(Serializable)data.problemAttachments);
                    intent.putExtras(bundle);
                    startActivity(intent);
                }else{
                    if(item.extension.equals(".mp4")){
                        Intent it = new Intent(mContext, VideosActivity.class);
                        it.putExtra("url", item.url);
                        it.putExtra("name", "视频详情");
                        startActivity(it);
                    }else{
                        Intent it = new Intent(mContext, PreImageActivity.class);
                        it.putExtra("url", item.url);
                        it.putExtra("name", "图片详情");
                        startActivity(it);
                    }

                }
            }
        });


    }

    @OnClick(R.id.iv_toolbar_back)
    public void onViewClicked() {
        finish();
    }

}
